
var _ifrm_minus_height = 48;
$(function()
{
	// 验证码
	$("#img_verify").click(function()
	{
		var url = _path + "/app/verify" + "?" + new Date().getTime();
		$(this).attr("src", url);
	});

	// 全屏
	var $fullText = $('.admin-fullText');
	$('#admin-fullscreen').on('click', function()
	{
		$.AMUI.fullscreen.toggle();
	});
	$(document).on($.AMUI.fullscreen.raw.fullscreenchange, function()
	{
		$fullText.text($.AMUI.fullscreen.isFullscreen ? '退出全屏' : '开启全屏');
	});

	// 标签
	var $tab = $('#div_tabs');
	var $nav = $tab.find('.am-tabs-nav');
	var $bd = $tab.find('.am-tabs-bd');
	function addTab(id, title, url)
	{
		var obj = {id: id, title: title, url: url};
		var opened = false;
		var index = 0;
		$.each($nav.find("li"), function(i, obj)
		{
			var pos = $(obj).attr("pos");
			if (pos == id)
			{
				opened = true;
				return false;
			}
			index++;
		});

		if (opened)
		{
			$tab.tabs("open", index);
		}
		else
		{
			var nav = "<li id=\"tab_{id}\" pos=\"{id}\"><span class=\"am-icon-close\"></span><a href=\"javascript: void(0)\">{title}</a></li>";
			var content = "<div class=\"am-tab-panel\"><iframe width=\"100%\" id=\"ifrm_{id}\"" +
					"height=\"100%\" frameborder=\"0\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" src=\"{url}\"></iframe></div>";

			$nav.addTemplate(obj, nav);
			$bd.addTemplate(obj, content);
			$tab.tabs('refresh').tabs("open", index);
			$("#ifrm_" + id).height($("#div_tabs").height() - _ifrm_minus_height);
		}
	}
	// 动态添加标签页
	$("#topbar-collapse a[url]").on("click", function()
	{
		var $this = $(this);
		var url = $this.attr("url");
		var parentPos = $this.attr("parent_pos");
		if (url && url != "#")
		{
			addTab($this.attr("pos"), $this.text(), url);
		}
		if (url)
		{
			$("#li_" + parentPos).dropdown("close");
		}
	});
	// 移除标签页
	$nav.on('click', '.am-icon-close', function()
	{
		var $item = $(this).closest('li');
		var index = $nav.children('li').index($item);

		$item.remove();
		$bd.find('.am-tab-panel').eq(index).remove();

		$tab.tabs('open', index > 0 ? index - 1 : index + 1);
		$tab.tabs('refresh');
	});

	$("#ifrm_index").height($("#div_tabs").height() - _ifrm_minus_height);
	$(window).resize(function()
	{
		$.each($bd.find("iframe"), function(i, obj)
		{
			$(obj).height($("#div_tabs").height() - _ifrm_minus_height);
		});
	});

	$("#div_confirm").on("closed.modal.amui", function()
	{
		$(this).removeData("amui.modal");
	});

});
